﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Drawing;
using System.Threading;
using Google.Protobuf.WellKnownTypes;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 外协挑库_提交前拆车数量校验
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CRM_WXSplitCarCheckController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;


        public CRM_WXSplitCarCheckController(ILogger<CRM_WXSplitCarCheckRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<CRM_WXSplitCarCheckController>();

        }

        /// <summary>
        /// 外协挑库_提交前拆车数量校验
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostSplitCarCheck([FromBody] CRM_WXSplitCarCheckRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            CRM_WXSplitCarCheckResponseDto re = new CRM_WXSplitCarCheckResponseDto();
            string shipment_no = "";
            string sysid = "";

            shipment_no = strparm.shipment_no;
            if (strparm.shipment_no == "")
            {
                
                strMsg = "shipment_no不能为空，运算失败！";
            }
            if (strparm.data ==null)
            {

                strMsg = "出库明细不能为空！";
            }

            if (shipment_no == null)
            {
                sysid = "TMS0154";
            }
            else
            {
                sysid = "TMS0148";
            }


            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                if (strMsg == "")
                {
                    int i = 1;
                    foreach (var item in strparm.data)
                    {
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("plan_no_id", item.plan_no_id);
                        dic.Add("shipment_no", shipment_no);
                        DataTable dt_check = MethodHelp.GetTableInfo(sysid, dic, con);//查询计划总数量
                        if (dt_check.Rows.Count > 0)
                        {
                            double qty_s = DB.ToDouble(dt_check.Rows[0]["ship_qty"].ToString()) + DB.ToDouble(item.ship_qty);
                            double so_qty = DB.ToDouble(dt_check.Rows[0]["materialamount"].ToString());

                            if (qty_s > so_qty)
                            {
                                strMsg = strMsg + "-计划流水<" + item.plan_no_id.Trim().ToString() + ">，计划流水总数量<" + so_qty + ">,已发运+本次数量<" + qty_s + ">,禁止超计划量派车！";
                            }
                        }
                        //找不到代表只有当前单据号的数据
                        //else
                        //{
                        //    strMsg = strMsg + "-找不到计划流水<" + item.plan_no_id.Trim().ToString() + ">相关信息，请确认后再试！";
                        //}
                    }
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
